package com.ltxtiyu.video.tool;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.net.URLEncoder;

public class SmsTool {
    
    public static final String SMS_URL_4_ZT = "http://www.ztsms.cn:8800/sendManySms.do";
    public static final String SMS_ACCOUNT_4_ZT = "xinhexing";
    public static final String SMS_PASSWORD_4_ZT = "Kddrsbd666";
    public static final String SMS_URL_NOTIFICATION_4_ZT = "676767";
    public static final String SMS_CHANNEL_VALIDATION_4_ZT = "676766";
    public static final String SMS_SIGN_4_ZT = "【火石镜头】";
    public static final String SMS_SIGN_5_ZT = "【火石镜头】";
    
    public static final String SMS_URL_4_CHANZOR = "http://sms.chanzor.com:8001/sms.aspx?action=send";
    public static final String SMS_ACCOUNT_4_CHANZOR = "kaidanwang";
    public static final String SMS_PASSWORD_4_CHANZOR = "kd2016w";
    public static final String SMS_SIGN_4_CHANZOR = "【火石镜头】";

    public static final String SMS_DEFAULT_CHANNEL = "chanzor";
    public static final String SMS_CAPTCHA_CHANNEL = "zhutong";
    
    
    private final static Logger LOGGER = LoggerFactory.getLogger(SmsTool.class);
    /**
     * 缺省用通知通道
     * 
     * @param mobile
     * @param content
     * @return
     */
    public static boolean sendSmsDefault(String mobile, String content) {
        if("chanzor".equals(SMS_DEFAULT_CHANNEL)){
            String rs = sendSmsByChanzor(mobile, content);
            try {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(new ByteArrayInputStream(rs
                        .getBytes("UTF-8")));
                Element foo = doc.getRootElement();
                Element e = foo.getChild("returnstatus");
                if ("Success".equals(e.getValue())) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            LOGGER.info(rs);
        }else{
            String rs = SmsTool.sendSms(mobile, content,
                    SMS_URL_NOTIFICATION_4_ZT);
            if (rs.startsWith("1")) {
                return true;
            }
            LOGGER.info(rs);
        }
        return false;
    }
    
    /**
     * 缺省用通知通道
     * 迪迪好贷
     * @param mobile
     * @param content
     * @return
     */
    public static boolean sendDDHDSmsDefault(String mobile, String content) {
        if("chanzor".equals(SMS_DEFAULT_CHANNEL)){
            String rs = sendSmsByChanzor(mobile, content);
            try {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(new ByteArrayInputStream(rs
                        .getBytes("UTF-8")));
                Element foo = doc.getRootElement();
                Element e = foo.getChild("returnstatus");
                if ("Success".equals(e.getValue())) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            LOGGER.info(rs);
        }else{
            String rs = SmsTool.sendDDHDSms(mobile, content,
                    SMS_URL_NOTIFICATION_4_ZT);
            if (rs.startsWith("1")) {
                return true;
            }
            LOGGER.info(rs);
        }
        return false;
    }
    
    /**
     * zt短信通道
     * 迪迪好贷
     * @param mobile
     * @param content
     * @param productid
     * @return
     */
    private static String sendDDHDSms(String mobile, String content, String productid) {

        String rs = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            StringBuffer sb = new StringBuffer(SMS_URL_4_ZT);
            sb.append("?username=");
            sb.append(SMS_ACCOUNT_4_ZT);
            sb.append("&password=");
            sb.append(DigestUtils.md5Hex(SMS_PASSWORD_4_ZT));
            sb.append("&mobile=");
            sb.append(mobile);
            sb.append("&content=");
            sb.append(content + SMS_SIGN_5_ZT);
            sb.append("&productid=");
            sb.append(productid);

            HttpGet get = new HttpGet(sb.toString());
            HttpResponse response = httpclient.execute(get);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                rs = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 验证码专用通道
     * 
     * @param mobile
     * @param content
     * @return
     */
    public static boolean sendSmsCaptcha(String mobile, String content) {
        if("chanzor".equals(SMS_CAPTCHA_CHANNEL)){
            String rs = sendSmsByChanzor(mobile, content);
            try {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(new ByteArrayInputStream(rs
                        .getBytes("UTF-8")));
                Element foo = doc.getRootElement();
                Element e = foo.getChild("returnstatus");
                if ("Success".equals(e.getValue())) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            String rs = SmsTool.sendSms(mobile, content,
                    SMS_CHANNEL_VALIDATION_4_ZT);
            if (rs.startsWith("1")) {
                return true;
            }
        }
        return false;
    }

    /**
     * zt短信通道
     * 
     * @param mobile
     * @param content
     * @param productid
     * @return
     */
    private static String sendSms(String mobile, String content, String productid) {

        String rs = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            StringBuffer sb = new StringBuffer(SMS_URL_4_ZT);
            sb.append("?username=");
            sb.append(SMS_ACCOUNT_4_ZT);
            sb.append("&password=");
            sb.append(DigestUtils.md5Hex(SMS_PASSWORD_4_ZT));
            sb.append("&mobile=");
            sb.append(mobile);
            sb.append("&content=");
            sb.append(content + SMS_SIGN_4_ZT);
            sb.append("&productid=");
            sb.append(productid);

            HttpGet get = new HttpGet(sb.toString());
            HttpResponse response = httpclient.execute(get);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                rs = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    
    /**
     * chanzor短信通道
     * 
     * @param mobile 多个号码之间用半角逗号
     * @param content
     * @return
     */
    private static String sendSmsByChanzor(String mobile, String content) {

        String rs = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            StringBuffer sb = new StringBuffer(SMS_URL_4_CHANZOR);
            sb.append("&account=");
            sb.append(SMS_ACCOUNT_4_CHANZOR);
            sb.append("&password=");
            sb.append(SMS_PASSWORD_4_CHANZOR);
            sb.append("&mobile=");
            sb.append(mobile);
            sb.append("&content=");
            sb.append(URLEncoder.encode(content + SMS_SIGN_4_CHANZOR, "utf-8"));

            HttpGet get = new HttpGet(sb.toString());
            HttpResponse response = httpclient.execute(get);

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                rs = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    
}